package com.gewu.pm.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 查看单据页面
 */
public final class ViewDocumentsActivity extends AppActivity {

    // 标签
    private TextView tvAccountPeriodLabel;
    private TextView tvOrderDateLabel;
    private TextView tvRelatedOrdersLabel;
    private TextView tvPeriodAmountLabel;

    // 值
    private TextView tvAccountPeriodValue;
    private TextView tvOrderDateValue;
    private TextView tvRelatedOrdersValue;
    private TextView tvPeriodAmountValue;
    private TextView tvTotalAmount;

    // 数据
    private String accountPeriodNumber;
    private String orderDate;
    private List<String> relatedOrders;
    private String periodAmount;

    /**
     * 启动方法
     */
    public static void start(Context context) {
        Intent intent = new Intent(context, ViewDocumentsActivity.class);
        context.startActivity(intent);
    }

    /**
     * 带参数启动方法
     */
    public static void start(Context context, String accountPeriod) {
        Intent intent = new Intent(context, ViewDocumentsActivity.class);
        intent.putExtra("account_period", accountPeriod);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_documents_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        // 标签
        tvAccountPeriodLabel = findViewById(R.id.tv_account_period_label);
        tvOrderDateLabel = findViewById(R.id.tv_order_date_label);
        tvRelatedOrdersLabel = findViewById(R.id.tv_related_orders_label);
        tvPeriodAmountLabel = findViewById(R.id.tv_period_amount_label);

        // 值
        tvAccountPeriodValue = findViewById(R.id.tv_account_period_value);
        tvOrderDateValue = findViewById(R.id.tv_order_date_value);
        tvRelatedOrdersValue = findViewById(R.id.tv_related_orders_value);
        tvPeriodAmountValue = findViewById(R.id.tv_period_amount_value);
        tvTotalAmount = findViewById(R.id.tv_total_amount);
    }

    @Override
    protected void initData() {
        // 获取传入的数据
        Intent intent = getIntent();
        accountPeriodNumber = intent.getStringExtra("account_period");

        // 如果没有传入数据，使用模拟数据
        if (TextUtils.isEmpty(accountPeriodNumber)) {
            loadMockData();
        }

        // 显示数据
        displayData();
    }

    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        accountPeriodNumber = "FH202508130002";
        orderDate = "2025-08-01~2025-08-11";
        relatedOrders = new ArrayList<>();
        relatedOrders.add("SD202508120001");
        relatedOrders.add("SD202508120001");
        relatedOrders.add("SD202508120001");
        periodAmount = "¥3000.00";
    }

    /**
     * 显示数据
     */
    private void displayData() {
        // 账期单号
        tvAccountPeriodValue.setText(accountPeriodNumber);

        // 订单日期
        tvOrderDateValue.setText(orderDate);

        // 关联订单（换行显示）
        if (relatedOrders != null && !relatedOrders.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < relatedOrders.size(); i++) {
                sb.append(relatedOrders.get(i));
                if (i < relatedOrders.size() - 1) {
                    sb.append("\n");
                }
            }
            tvRelatedOrdersValue.setText(sb.toString());
        }

        // 账期金额
        tvPeriodAmountValue.setText(periodAmount);

        // 底部合计
        tvTotalAmount.setText(periodAmount);
    }
}

